package com.luofan.service.impl;

import com.luofan.common.result.ResultCodeEnum;
import com.luofan.entity.po.Term;
import com.luofan.mapper.TermMapper;
import com.luofan.service.TermService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 学期表 服务实现类
 * </p>
 *
 * @author 
 * @since 2025-03-26
 */
@Service
public class TermServiceImpl extends ServiceImpl<TermMapper, Term> implements TermService {

    @Autowired
    private TermMapper termMapper;

    //创建新学期信息
    @Override
    public ResultCodeEnum createTerm(Term term) {
        //通过年份和半年查询学期是否存在
        int count = termMapper.selectTermByYearAndSemester(term.getYear(), term.getSemester());
        //存在则返回错误信息
        if (count > 0) return ResultCodeEnum.EXIST_ERROR;
        //创建新学期信息
        int createTermRows = termMapper.createTerm(term);
        if (createTermRows == 0) return ResultCodeEnum.FAIL;
        return ResultCodeEnum.SUCCESS;
    }

    //修改学期的开始时间和结束时间
    @Override
    public void updateTerm(Term term) {
        termMapper.updateTerm(term);
    }

    //根据当前时间获取当前学期信息
    @Override
    public Term getTermIdByTime() {
        //获取当前时间
        LocalDateTime now = LocalDateTime.now();
        //调用查询方法
        return termMapper.getTermIdByTime(now);
    }
}
